Flutter / UI Elements / Image - Network
Image
-
Code
Syntax
const Image( image: NetworkImage('https://www.tutorialkart.com/img/lion.jpg'), ) import 'dart:ui'; import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); static const String _title = 'Flutter Tutorial'; @override Widget build(BuildContext context) { return MaterialApp( title: _title, home: Scaffold( appBar: AppBar(title: const Text(_title)), body: const MyStatefulWidget(), ), ); } } class MyStatefulWidget extends StatefulWidget { const MyStatefulWidget({Key? key}) : super(key: key); @override State createState() => _MyStatefulWidgetState(); } class _MyStatefulWidgetState extends State { @override Widget build(BuildContext context) { return Center( child: Column( children: [ Container( margin: const EdgeInsets.all(20), child: const Image( image: NetworkImage( 'https://www.tutorialkart.com/img/lion.jpg'), ), ), ], ), ); } } -
Basic Usage
1. Image Widget:
Image( image: NetworkImage('https://example.com/image.jpg'), ) 2. CircleAvatar Widget:
CircleAvatar( backgroundImage: NetworkImage('https://example.com/avatar.jpg'), ) 3. DecorationImage in BoxDecoration:
Container( decoration: BoxDecoration( image: DecorationImage( image: NetworkImage('https://example.com/background.jpg'), fit: BoxFit.cover, ), ), ) 4. CachedNetworkImage Widget:
CachedNetworkImage( imageUrl: 'https://example.com/image.jpg', placeholder: (context, url) => CircularProgressIndicator(), errorWidget: (context, url, error) => Icon(Icons.error), ) -
Usage in layouts
1. Column and Row Layouts:
Column( children: [ Image( image: NetworkImage('https://example.com/image1.jpg'), ), Image( image: NetworkImage('https://example.com/image2.jpg'), ), ], ) 2. GridView:
GridView.builder( gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 2, crossAxisSpacing: 4.0, mainAxisSpacing: 4.0, ), itemCount: imageUrls.length, itemBuilder: (BuildContext context, int index) { return Image( image: NetworkImage(imageUrls[index]), ); }, ) 3. ListView:
ListView.builder( itemCount: imageUrls.length, itemBuilder: (BuildContext context, int index) { return Image( image: NetworkImage(imageUrls[index]), ); }, ) 4. Stack Layout:
Stack( children: [ Image( image: NetworkImage('https://example.com/background.jpg'), fit: BoxFit.cover, height: double.infinity, width: double.infinity, ), Positioned( top: 20.0, left: 20.0, child: Image( image: NetworkImage('https://example.com/foreground.jpg'), ), ), ], )